// // pages/open_per/open_tmppwd/open_tmppwd.js
// const md5 = require('../../../utils/md5.js');
const formatTime1 = require('../../../utils/util.js').formatTime1;
const app = getApp();


const date = new Date()
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
for (let i = 2019; i <= 2100; i++) {
  years.push(i)
}
for (let i = 1; i <= 12; i++) {
  months.push(i)
}
for (let i = 1; i <= 31; i++) {
  days.push(i)
}
for (let i = 0; i <= 23; i++) {
  hours.push(i)
}
for (let i = 0; i <= 59; i++) {
  minutes.push(i)
}

 Page({

   /**
    * 页面的初始数据
    */
  data: {
    startTime:[],
    endTime:[],
    startTime1: [],
    endTime1: [],
    isStart:null,                //true:选择开始时间；false：选择结束时间
    isduration: false,           //自定义时效弹窗
    isTime:true,                //设置时间弹窗
    timeValue: [0, 0, 0, 0, 0],                     //当前起始时间（year,months,days,hours,minutes的下标）
    timeValue1: [0, 0, 0, 0, 0],                    //当前起始时间（year,months,days,hours,minutes的下标，临时）

    years: years,
    months: months,
    days: days,
    hours: hours,
    minutes: minutes,

    devIndex:0,
    deviceName: null,
    deviceNick: null,
    isgetpwd:true,                              //获取管理员密码界面
    pass:null,                                 //管理员密码
//     startTime: null,                             //开始生效时间
//     endTime: null,                               //结束生效时间
//     time: null,                                  //计算临时密码所需时间
//     tmppwd: [],                                  //临时密码

//     isduration: false,                           //选择生效时长
//     durations: ['一小时', '一天', '一月', '一年'],  //选择生效时长-选项值
//     duration: [0],                                  //当前生效时长（durations的下标）
//     duration1: [0],                                 //当前生效时长（durations的下标,临时）

//     istime: false,                               //选择起始时间
//     timeValue: [0, 0, 0, 0],                     //当前起始时间（year,months,days,hours的下标）
//     timeValue1: [0, 0, 0, 0],                    //当前起始时间（year,months,days,hours的下标，临时）
//     years: years,
//     months: months,
//     days: days,
//     hours: hours,
//     hasYear:false
   },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var date = new Date();
    var date1 = new Date();
    date1.setMinutes(date1.getMinutes()+10)            //默认结束时间为开始时间10分钟后
    that.data.devIndex = options.index,
    that.setData({
      startTime: formatTime1(date),
      endTime: formatTime1(date1)
    })
    that.data.deviceName = options.deviceName;
    that.data.deviceNick = options.deviceNick;
  //  that.data.deviceNick = app.globalData.kvStorage.deviceList[options.index].deviceNick;
  },

/************************** 修改生效时段按钮 *************************/
  changeDuration:function(){
    var that = this;
    that.setData({
      startTime1: that.data.startTime,
      endTime1: that.data.endTime,
      isduration:true,
      istime: false,
    })
  },
/************************** 自定义时效 *************************/
// 确定
  confirmDuration: function (){
    var that = this;
    var diff = [0,0,0,0,0];
    var isNegative = false;
    var startTime = that.data.startTime1;
    var endTime = that.data.endTime1;
    for(let i = 0; i <= 4; i++){
      diff[i] = endTime[i] - startTime[i];
    }
    if ((diff[0] > 0) || (diff[0] == 0 && diff[1] > 0) || (diff[0] == 0 && diff[1] == 0 && diff[2] > 0) || (diff[0] == 0 && diff[1] == 0 && diff[2] == 0 && diff[3] > 0) || (diff[0] == 0 && diff[1] == 0 && diff[2] == 0 && diff[3] == 0&& diff[4] > 0)){
      that.setData({
        isduration: false,
        startTime: startTime,
        endTime: endTime
      })
    }
    else{
      isNegative = true;
      wx.showToast({
        title: '结束时间必须大于开始时间',
        icon: 'none',
        duration: 2000
      })
    }
    
  },

// 取消
  cancelDuration: function () { 
    var that = this;
    that.setData({
      isduration: false,
      istime: false
    })
  },

/************************** 选择时间 *************************/
//开始
  startTime:function(){
    var that = this;
    that.setData({
      isStart : true
    });
    that.time(that.data.startTime1)
  },
//结束
  endTime: function () {
    var that = this;
    that.setData({
      isStart: false
    });
    that.time(that.data.endTime1)
  },
//选择时间
  time:function(date){
    var that = this;
    that.setData({
      isduration: false,
      istime: true,
      "timeValue[0]": date[0]-2019,
      "timeValue[1]": date[1]-1,
      "timeValue[2]": date[2]-1,
      "timeValue[3]": date[3],
      "timeValue[4]": date[4],
    })
  },
//选择时间
  timeChange:function(e){
    console.log(e.detail.value);
    this.data.timeValue1 = e.detail.value
    this.days(e.detail.value[0] + 2019, e.detail.value[1]+1);
  },
//计算本月天数
  days:function(year,month){
    var that = this;
    var d = new Date(year, month, 0);
    var days = [];
    for (let i = 1; i <= d.getDate(); i++) {
      days.push(i)
    }
    that.setData({days:days});
  },
//确定
  confirmTime:function(){
    var that = this;
    that.setData({
      timeValue:that.data.timeValue1,
      istime: false,
      isduration:true,
    })
    if(that.data.isStart){
      that.setData({
        "startTime1[0]": that.data.timeValue1[0] + 2019,
        "startTime1[1]": that.data.timeValue1[1] + 1,
        "startTime1[2]": that.data.timeValue1[2] + 1,
        "startTime1[3]": that.data.timeValue1[3],
        "startTime1[4]": that.data.timeValue1[4],
      })
    }
    else{
      that.setData({
        "endTime1[0]": that.data.timeValue1[0] + 2019,
        "endTime1[1]": that.data.timeValue1[1] + 1,
        "endTime1[2]": that.data.timeValue1[2] + 1,
        "endTime1[3]": that.data.timeValue1[3],
        "endTime1[4]": that.data.timeValue1[4]
      })
    }
  },
//返回
  back:function(){
    var that = this;
    that.setData({
      isduration: true,
      istime: false
    })
  },
/************************** 分享 *************************/
  onShareAppMessage: function (options) {
    console.log(options)
    return{
      path: `/pages/open_per/tmppwd_share/tmppwd_share?startTime=${this.data.startTime}&endTime=${this.data.endTime}&deviceName=${this.data.deviceName}&deviceNick=${this.data.deviceNick}&pass=${this.data.pass}`
    }
  },
//   /************************** 计算临时密码 *************************/
//   tmppwd: function (duration,startTime){
//     var that = this;
//     var time;
//     if (duration == 0){
//       time = (startTime[0]).toString().substr(2, 2) + ((startTime[1].toString().length == 2) ? (startTime[1]) : ('0' + startTime[1])) + ((startTime[2].toString().length == 2) ? (startTime[2]) : ('0' + startTime[2])) + ((startTime[3].toString().length == 2) ? (startTime[3]) : ('0' + startTime[3]))
//     }
//     else if (duration == 1) { 
//       time = (startTime[0]).toString().substr(2, 2) + ((startTime[1].toString().length == 2) ? (startTime[1]) : ('0' + startTime[1])) + ((startTime[2].toString().length == 2) ? (startTime[2]) : ('0' + startTime[2]))
//       }
//     else if (duration == 2) {
//       time = (startTime[0]).toString().substr(2, 2) + ((startTime[1].toString().length == 2) ? (startTime[1]) : ('0' + startTime[1]))
//      }
//     else if (duration == 3) {
//       time = (startTime[0]).toString().substr(2, 2)
//      }
//     var plaintext = `${time}` + `${that.data.pass}`
//     var md5text = md5.hexMD5(plaintext)
//     var tmppwd = [];
//     for(let i = 0; i < 12; i = i+2){
//       var str = parseInt(md5text.substr(i, 2), 16)%10;
//       tmppwd.push(str)
//     }
//     that.setData({ tmppwd: tmppwd})
//     return tmppwd;
//   },
//   /************************** 修改生效时段 *************************/
//   changeDuration:function(){
//     var that = this;
//     var date = new Date();
//     that.setData({
//       duration: [0],
//       duration1: [0],
//       timeValue: [0, 0, 0, 0],
//       timeValue1: [0, 0, 0, 0],
//       isduration:true,
//       istime: false,
//     }),
//       that.data.startTime = formatTime1(date)
//   },
//   /************************** 选择生效时长 *************************/
//   durationChange:function(e){
//     console.log(e.detail.value); 
//     this.data.duration1 = e.detail.value
//   },
//   confirmDuration: function (){
//     var that = this;
//     that.setData({
//       duration: that.data.duration1,
//       isduration: false,
//       istime: true,
//       "timeValue[0]": that.data.startTime[0]-2019,
//       "timeValue[1]": (that.data.duration1 <= 2) ? (that.data.startTime[1]-1) : 1,
//       "timeValue[2]": (that.data.duration1 <= 1) ? (that.data.startTime[2]-1) : 1,
//       "timeValue[3]": (that.data.duration1 == 0) ? (that.data.startTime[3]) : 0,
//     })
//     that.data.timeValue1 = that.data.timeValue;
//     that.days(that.data.startTime[0], that.data.startTime[1]);
//   },
//   cancelDuration: function () { 
//     var that = this;
//     that.setData({
//       duration1:that.data.duration,
//       isduration: false,
//       istime: false
//     })
//   },
//   /************************** 选择起始时间 *************************/
//   timeChange:function(e){
//     console.log(e.detail.value);
//     this.data.timeValue1 = e.detail.value
//     if (e.detail.value.length>=2){
//       this.days(e.detail.value[0] + 2019, e.detail.value[1]+1);
//     }
//   },
//   back:function(){
//     var that = this;
//     that.setData({
//       isduration: true,
//       istime: false
//     })
//   },
//   confirmTime:function(){
//     var that = this;
//     that.setData({
//       timeValue:that.data.timeValue1,
//       istime: false,
//       "startTime[0]": that.data.timeValue1[0]+2019,
//       "startTime[1]": (that.data.duration1 <= 2) ? (that.data.timeValue1[1] + 1) : 1,
//       "startTime[2]": (that.data.duration1 <= 1) ? (that.data.timeValue1[2] + 1) : 1,
//       "startTime[3]": (that.data.duration1 == 0) ? (that.data.timeValue1[3]) : 0 ,
//     })
//     that.endtime(that.data.duration, that.data.startTime);
//     that.tmppwd(that.data.duration, that.data.startTime);
//   },

//   /************************** 计算结束时间 *************************/
//   endtime: function (duration,starttime){
//     var that = this;
//     var endtime = starttime.slice(0);
//     if (duration[0]==0){     //时效为一小时
//       endtime[3]++;
//       if (endtime[3]==24){    //转天
//         endtime[3] = 0;
//         endtime[2]++;
//         console.log(that.data.days[that.data.days.length-1])
//         if (endtime[2] == (that.data.days[that.data.days.length - 1]+1)){//转月
//         console.log("转月")
//           endtime[2] = 1;
//           endtime[1]++;
//           if (endtime[1] == 13){//转年
//             endtime[1] = 1;
//             endtime[0]++;
//           }
//         } 
//       }
//     }
//     else if (duration[0] == 1){  //时效为一天
//       endtime[3] = 0;
//       endtime[2]++;
//       if (endtime[2] == (that.data.days[that.data.days.length - 1] + 1)) {//转月
//         endtime[2] = 1;
//         endtime[1]++;
//         if (endtime[1] == 13) {//转年
//           endtime[1] = 1;
//           endtime[0]++;
//         }
//       }
//     }
//     else if (duration[0] == 2) {         //时效为一个月
//       endtime[3] = 0;
//       endtime[2] = 1;
//       endtime[1]++;
//       if (endtime[1] == 13) {//转年
//         endtime[1] = 1;
//         endtime[0]++;
//       }
//     }
//     else if (duration[0] == 3) {         //时效为一年
//       endtime[3] = 0;
//       endtime[2] = 1;
//       endtime[1] = 1;
//       endtime[0] ++;
//     }
//     that.setData({ endTime: endtime})
//   },
//   /************************** 计算本月天数 *************************/
//   days:function(year,month){
//     var that = this;
//     var d = new Date(year, month, 0);
//     var days = [];
//     for (let i = 1; i <= d.getDate(); i++) {
//       days.push(i)
//     }
//     that.setData({days:days});
//   },
  /************************** 提交管理员密码 *************************/
  formsubmit:function(e){
    console.log(e.detail.value.pwd)
    if (e.detail.value.pwd.length == 6){
      var that = this;
      that.data.pass = e.detail.value.pwd;
      var date = new Date();
      that.setData({
        isgetpwd: false,
        // startTime: formatTime1(date)
      })
      // that.endtime(that.data.duration, that.data.startTime);
      // that.tmppwd(that.data.duration, that.data.startTime);
    } 
    else{
      wx.showToast({
        title: '请输入六位管理员密码',
        icon:'none'
      })
    }
  },
  cancel:function(){
    wx.navigateBack({
      delta: 1
    })
  },

  
//   /**
//    * 生命周期函数--监听页面初次渲染完成
//    */
//   onReady: function () {

//   },

//   /**
//    * 生命周期函数--监听页面显示
//    */
//   onShow: function () {

//   },

//   /**
//    * 生命周期函数--监听页面隐藏
//    */
//   onHide: function () {

//   },

//   /**
//    * 生命周期函数--监听页面卸载
//    */
//   onUnload: function () {

//   },

//   /**
//    * 页面相关事件处理函数--监听用户下拉动作
//    */
//   onPullDownRefresh: function () {

//   },

//   /**
//    * 页面上拉触底事件的处理函数
//    */
//   onReachBottom: function () {

//   },

//   /**
//    * 用户点击右上角分享
//    */
//   onShareAppMessage: function (options) {
//     console.log(options)
//     return{
//       path: `/pages/open_per/tmppwd_share/tmppwd_share?startTime=${this.data.startTime}&endTime=${this.data.endTime}&tmppwd=${this.data.tmppwd}`
//     }
   
//   }
 })